package cz.edukomplex.kosilka.client.content.harmonogram;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.fredhat.gwt.xmlrpc.client.XmlRpcClient;
import com.fredhat.gwt.xmlrpc.client.XmlRpcRequest;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NodeList;

import cz.edukomplex.kosilka.client.helpers.MyRequestBuilder;
import cz.edukomplex.kosilka.client.helpers.MyXMLParser;
import cz.edukomplex.kosilka.client.helpers.MyXmlRpcClient;

public class HarmonogramAddDialog extends DialogBox {
	
	private HarmonogramContent parent;
	
	public TextBox nameTextBox = new TextBox();
	public ListBox commissionsListBox = new ListBox();
	public ListBox studyFieldListBox = new ListBox();
	public DateBox dateBox = new DateBox();
	
	
	public HarmonogramAddDialog(HarmonogramContent parent) {
		this.parent = parent;
		initCommissionListBox();
		initStudyFieldListBox();
		setAutoHideEnabled(true);
		setModal(false);
		setHTML("Vytvoriť harmonogram");
		
		FormPanel formPanel = new FormPanel();
		setWidget(formPanel);
		formPanel.setSize("100%", "100%");
		
		Grid grid = new Grid(5, 2);
		formPanel.setWidget(grid);
		grid.setSize("100%", "100%");
		
		Label lblMeno = new Label("Meno");
		grid.setWidget(0, 0, lblMeno);
		
		nameTextBox = new TextBox();
		grid.setWidget(0, 1, nameTextBox);
		
		Label lblKomisia = new Label("Komisia");
		grid.setWidget(1, 0, lblKomisia);
		
		commissionsListBox = new ListBox();
		grid.setWidget(1, 1, commissionsListBox);
		
		Label lblStudyField = new Label("Obor");
		grid.setWidget(2, 0, lblStudyField);
		
		studyFieldListBox = new ListBox();
		grid.setWidget(2, 1, studyFieldListBox);		
		
		Label lblDtum = new Label("Dátum");
		grid.setWidget(3, 0, lblDtum);
		
		dateBox = new DateBox();
		grid.setWidget(3, 1, dateBox);
		dateBox.setWidth("");
		
		Button btnPridat = new Button("Pridať");
		btnPridat.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				saveForm();				
			}
		});
		grid.setWidget(4, 1, btnPridat);
		grid.getCellFormatter().setHorizontalAlignment(4, 1, HasHorizontalAlignment.ALIGN_RIGHT);		
		center();		
		nameTextBox.setFocus(true);
		
	}

	private void initStudyFieldListBox() {
		XmlRpcClient client = MyXmlRpcClient.createClient();
		String methodName = "obor.fetchAll";
		
		XmlRpcRequest<ArrayList<HashMap<String, String>>> request = new XmlRpcRequest<ArrayList<HashMap<String,String>>>(
				client, methodName, null, new AsyncCallback<ArrayList<HashMap<String, String>>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(
							ArrayList<HashMap<String, String>> result) {
						
						for (@SuppressWarnings("rawtypes")
						Iterator iterator = result.iterator(); iterator
								.hasNext();) {
							@SuppressWarnings("unchecked")
							HashMap<String, String> hashMap = (HashMap<String, String>) iterator
									.next();
						
							
							studyFieldListBox.addItem(
									hashMap.get("Obor"), 
									hashMap.get("Id_oboru")									
							);
						}
						
					}
		});
		request.execute();		
	}

	private void initCommissionListBox() {
		XmlRpcClient client = MyXmlRpcClient.createClient();
		String methodName = "commission.fetchAll";
		
		XmlRpcRequest<ArrayList<HashMap<String, String>>> request = new XmlRpcRequest<ArrayList<HashMap<String,String>>>(
				client, methodName, null, new AsyncCallback<ArrayList<HashMap<String, String>>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(
							ArrayList<HashMap<String, String>> result) {
						
						for (@SuppressWarnings("rawtypes")
						Iterator iterator = result.iterator(); iterator
								.hasNext();) {
							@SuppressWarnings("unchecked")
							HashMap<String, String> hashMap = (HashMap<String, String>) iterator
									.next();
						
							
							commissionsListBox.addItem(
									hashMap.get("name"), 
									hashMap.get("cid")									
							);
						}
						
					}
		});
		request.execute();		
	}

	protected void saveForm() {
		
		if(!nameTextBox.getValue().isEmpty()){
			
			final String name = nameTextBox.getText();			
			final String date = (dateBox.getValue() != null) ? String.valueOf(dateBox.getValue().getTime()) : "";
			final String commission = commissionsListBox.getValue(commissionsListBox.getSelectedIndex());
			final String idStudyField = studyFieldListBox.getValue(studyFieldListBox.getSelectedIndex());
			final String studyFieldName = studyFieldListBox.getItemText(studyFieldListBox.getSelectedIndex());
			
			
			XmlRpcClient client = MyXmlRpcClient.createClient();
			String methodName = "harmonogram.add";
			Object[] params = new Object[]{name,commission,date,idStudyField};
			
			XmlRpcRequest<Integer> addHarmonogramRequest = new XmlRpcRequest<Integer>(
					client, methodName, params, new AsyncCallback<Integer>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(Integer result) {
							Window.alert(result + "");
							if(result > 0){
								hide();									
								parent.addCellListRow(new HarmonogramCellListRow(
										String.valueOf(result),
										name, 
										commission, 
										commissionsListBox.getItemText(commissionsListBox.getSelectedIndex()),
										idStudyField,
										studyFieldName,
										date
										)
								);
							}
						}
			});
			
			addHarmonogramRequest.execute();
		}		
	}
}